#!/usr/bin/env python
# -*- coding:utf-8 -*- 
# Author: Jeokayne Ataier

import socket
server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server.bind(("127.0.0.1",8881))
server.listen(10)


conn,addr = server.accept()
print("已链接！")
#1.先传输文件大小信息
file_size = int(conn.recv(1024).decode("utf-8"))
print("文件大小：",file_size)

#2.开始接收并写入文件
filepath = open("xxx.png","wb")
recv_size = 0
while True:
    data = conn.recv(1024)
    filepath.write(data)
    filepath.flush()
    recv_size += len(data)

    #判断是否传输完成
    if recv_size == file_size:
        break


conn.close()
server.close()